﻿YAHOO.namespace("example.container");
YAHOO.util.Event.onDOMReady(function () {
    var handleSubmit = function () {
        ModifyPassword();
    };
    var handleCancel = function () {
        document.getElementById("oldPwd").value = "";
        document.getElementById("newPwd").value = "";
        document.getElementById("rNewPwd").value = "";
    };
    var handleSuccess = function (o) {
        var response = o.responseText;
        response = response.split("<!")[0];
        document.getElementById("resp").innerHTML = response;
    };
    var handleFailure = function (o) {
        alert("Submission failed: " + o.status);
    };
    YAHOO.util.Dom.removeClass("dialog1", "yui-pe-content");
    YAHOO.example.container.dialog1 = new YAHOO.widget.Dialog("dialog1",
							    { width: "350px",
							        height: "260px",
							        fixedcenter: false,
							        visible: true,
							        draggable: true,
							        close: false,
							        autofillheight: "body",
							        constraintoviewport: true,
							        buttons: [{ text: "保存", handler: handleSubmit, isDefault: true },
								          { text: "重置", handler: handleCancel}]
							    });
    YAHOO.example.container.dialog1.callback = { success: handleSuccess,
        failure: handleFailure
    };
    YAHOO.example.container.dialog1.render();
});

function ModifyPassword() {
    if (document.getElementById("oldPwd").value == "") {
        alert("原始密码不能为空!");
        document.getElementById("oldPwd").focus();
        return;
    } else if (document.getElementById("newPwd").value == "") {
        alert("新密码不能为空!");
        document.getElementById("newPwd").focus();
        return;
    } else if (document.getElementById("rNewPwd").value == "") {
        alert("确认新密码不能为空!");
        document.getElementById("rNewPwd").focus();
        return;
    } else if (document.getElementById("rNewPwd").value != document.getElementById("newPwd").value) {
        alert("两次输入密码不一致!");
        document.getElementById("newPwd").value = "";
        document.getElementById("rNewPwd").value = "";
        document.getElementById("newPwd").focus();
        return;
    } else {
        var olds = document.getElementById("oldPwd").value;
        var news = document.getElementById("rNewPwd").value;
        var userId = document.getElementById("hiddenUserId").value;
        if (userId == "") {
            alert("数据错误,修改密码失败!");
            window.parent.$("#tabs").tabs('close', '修改密码');
            return;
        } else {
            var url = "Extend/AjaxData/DataProcessin.aspx?action=Save&entity=DatUser&ids=" + userId + "&old=" + olds + "&news=" + news + "";
            YAHOO.util.Connect.asyncRequest('POST', url, {
                success: function (res) {
                    var inventory;
                    try {
                        inventory = res.responseText;
                        if (inventory == "1") {
                            alert("修改密码成功!");
                            window.parent.$("#tabs").tabs('close', '修改密码');
                        } else if (inventory == "0") {
                            alert("原始密码输入错误!请重新输入!");
                            document.getElementById("oldPwd").value = "";
                        } else {
                            alert(inventory);
                            return;
                        }
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("读取数据错误：" + error.statusText);
                }
            }, null);
        }
    }
}